class Solution
{
public:
    int minMalwareSpread(vector<vector<int>> &graph, vector<int> &initial)
    {
        int n = graph.size();
        auto bfs = [&](int exclude) -> int
        {
            vector<int> visited(n, 0);
            queue<int> q;
            for (int i : initial)
            {
                if (i != exclude)
                {
                    q.push(i);
                    visited[i] = 1;
                }
            }
            while (!q.empty())
            {
                int u = q.front();
                q.pop();
                for (int v = 0; v < n; ++v)
                {
                    if (v == exclude)
                    {
                        continue;
                    }
                    if (graph[u][v] && !visited[v])
                    {
                        visited[v] = 1;
                        q.push(v);
                    }
                }
            }
            return accumulate(visited.begin(), visited.end(), 0);
        };
        sort(initial.begin(), initial.end());
        int ans = initial[0], maxAffected = bfs(initial[0]);
        for (int i : initial)
        {
            int affected = bfs(i);
            if (affected < maxAffected)
            {
                ans = i;
                maxAffected = affected;
            }
        }
        return ans;
    }
};